package com.nsl.my_youlai.jackson;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.nsl.my_youlai.common.enums.DocxTemplateStatusEnum;
import com.nsl.my_youlai.mystar.enums.NslEnumBaseI;
import com.nsl.my_youlai.mystar.enums.NslEnumTransformException;

import java.io.IOException;
import java.util.Iterator;

public class DocxTemplateStatusEnumDeserializer extends StdDeserializer<DocxTemplateStatusEnum> {

    public DocxTemplateStatusEnumDeserializer() {
        this(null);

    }

    protected DocxTemplateStatusEnumDeserializer(Class<?> vc) {
        super(vc);
    }

    @Override
    public DocxTemplateStatusEnum deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JacksonException {


        //System.out.println(jsonParser.getCodec());
        JsonNode node = jsonParser.getCodec().readTree(jsonParser);
        Iterator<String> fieldNameIt=node.fieldNames();
        while ((fieldNameIt.hasNext())){
            System.out.println(fieldNameIt.next());
        }
        String label=node.get("label").asText();
        System.out.println("label = "+label);
        Integer value=node.get("value").intValue();
        try {
            return NslEnumBaseI.getEnumByValue(value,DocxTemplateStatusEnum.class);
        } catch (NslEnumTransformException e) {
            throw new RuntimeException(e);
        }


//        System.out.println("label = "+node.get("label").asText());
//        System.out.println("value = "+node.get("value").asText());

//        return IBaseEnum.getEnumByValue(0,NslEnumBaseI.class);

    }
}
